package ink.lovejinhu.swardToOffer;

import java.util.ArrayList;

/**
 * @Author: jin
 * @Date: 2021/8/30 23:53
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Offer_057II {
    /**
     * 输出所有和为target的组合
     * x1+x2+.....+xn=target
     * n(x1)+(1+...n-1)=target
     * n(x1)+(1+n-1)*(n-1)/2=target
     * n*x1+n*(n-1)/2=target
     * n*[x1+(n-1)/2]=target
     *
     * @param target
     * @return
     */
    public int[][] findContinuousSequence(int target) {
        ArrayList<int[]> res = new ArrayList<>();
        for (int i = 1; i < target / 2 + 1; i++) {
            for (double j = 2; j < target / 2 + 1; j++) {
                double v = j * (i + (j - 1) / 2);
                if (v > target) {
                    break;
                }
                if (v == target) {

                    int[] ints = new int[(int) j];
                    for (int k = i; k <= i + j - 1; k++) {
                        ints[k - i] = k;
                    }
                    res.add(ints);
                }

            }
        }
        return res.toArray(new int[res.size()][]);

//        for (double i = 2; i < target / 2 + 1; i++) {
//            for (int j = 1; j < target / 2 + 1; j++) {
//                double v = i * (j + (i - 1) / 2);
//                if (v > target) {
//                    break;
//                }
//                if (v == target) {
//                    ArrayList<Integer> integers = new ArrayList<>();
//                    for (int k = j; k <= j + i - 1; k++) {
//                        integers.add(k);
//                    }
//                    res.add(integers);
//                    break;
//                }
//            }
//        }
//        System.out.println(res);
//
//        return null;
    }

    public static void main(String[] args) {
        Offer_057II offer_057II = new Offer_057II();
        int[][] continuousSequence = offer_057II.findContinuousSequence(15);

    }
}
